//
//  CTFenceUserViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/6.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTFenceUserViewController.h"
#import "CTUserFenceView.h"
#import "CTTabBarController.h"
#import "CTUserViewController.h"

//config
#import "CTHeader.h"

//logic
#import "CTFenceUserLogic.h"
#import "CTLogic.h"

//view
#import "CTUserView.h"

//function
#import "CTDebugManager.h"
#import "CTGenRequestData.h"
#import "CTDataGenerator.h"
#import "MobClick.h"


@interface CTFenceUserViewController ()<CTUserFenceViewDelegate>
{
    CTUserFenceView *fenceTableView;
    CTFenceUserLogic * fenceUserLogic;
    
    CTLogic *logic;
}

@end

@implementation CTFenceUserViewController

@synthesize userUid,levelsType;

- (void)dealloc
{
    [fenceTableView removeFromSuperview];
    fenceTableView.delegate = nil;
    fenceTableView = nil;
    
    logic = nil;
    
    fenceUserLogic = nil;
    
    userUid = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_FOLLOWING_SUCCESS object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTFenceUserViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        fenceUserLogic = [[CTFenceUserLogic alloc] init];
        logic = [[CTLogic alloc] init];
        
        //关注成功，从新刷新数据库数据
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(followPersonSuccess:) name:CT_NOTI_FOLLOWING_SUCCESS object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelFollowPersonSuccess:) name:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //统计
    [MobClick beginLogPageView:@"CTFenceUserViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTFenceUserViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    fenceTableView = [[CTUserFenceView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT)];
    fenceTableView.delegate = self;
    [self.view addSubview:fenceTableView];
    // Do any additional setup after loading the view.
    
    [self sendFenceRequestList];
}


#pragma mark - request -

-(void)sendFenceRequestList
{
    [fenceUserLogic fenceUserListRequest:[CTGenRequestData genRequestFenceUserItem:userUid] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
          fenceTableView.fenceArray = [CTDataGenerator generateFenceUserListItems];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
    }];
}


#pragma mark - UserFenceViewDelegate -

- (void)userFenceTableViewBackBtnAction:(CTUserFenceView *) view
{
    if (levelsType == CTFenceUserViewControllerLevelsFirst) {
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC showTabbarWithAnimate:YES];
    }

    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)userFenceTableView:(CTUserFenceView *)tableView WithItme:(CTUserItem *)item
{
    CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
    [tabbarVC hideTabbarWithAnimate:YES];
    CTUserViewController * userVC = [[CTUserViewController alloc] init];
    userVC.pushType = CTUserViewControllerPushFromTypeOther;
    userVC.levelType = CTUserViewControllerLevelOther;
    userVC.userUid = item.uid;
    [self.navigationController pushViewController:userVC animated:YES];
}
- (void)userFenceTableViewAttentionBtnAction:(CTUserFenceView *) cell withItem:(CTUserItem *)item withBool:(BOOL)attenBool
{
    if (attenBool == YES) {
        [logic followPersonRequest:[CTGenRequestData genRequestFollowPseronItem:item.uid] successCompletionHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [CTDebugManager showDebugLogInfo:@"关注成功！"];
                  [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_FOLLOWING_SUCCESS object:nil];
            });
        } failedHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [CTDebugManager showErrorLogInfo:@"关注失败！"];
            });
        }];
    }else{
        [logic cancelFollowPersonRequest:[CTGenRequestData genRequestCancelFollowPseronItem:item.uid] successCompletionHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [CTDebugManager showDebugLogInfo:@"取消关注成功！"];
                 [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
            });
        } failedHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [CTDebugManager showErrorLogInfo:@"取消关注失败！"];
            });
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//刷新
-(void)refreshControlPullDown:(CTUserFenceView *)view
{
    [self sendFenceRequestList];
}
-(void)refreshControlPullUp:(CTUserFenceView *)view
{
    
}


#pragma mark - login & exit -
-(void)followPersonSuccess:(id)sender
{
     [self sendFenceRequestList];
}

-(void)cancelFollowPersonSuccess:(id)sender
{
    [self sendFenceRequestList];
}

@end
